/**
 * 拦截response，统一返回｛code：200，data：data｝
 */
angular.module('mamApp')
        .config(['$httpProvider', function ($httpProvider) {
            $httpProvider.interceptors.push([function () {
                return {
                    'request': function (config, body) {
                        var userInfo = JSON.parse(localStorage.getItem('userInfo'));
                        if(userInfo){
                            config.headers['X-Auth-Token'] = userInfo.token;
                        }else {
                            // localStorage.removeItem("userInfo");
                            // localStorage.setItem("orginUrl",window.location.href);
                            // window.location = 'http://' + window.location.host+'/#/login';
                        }

                        return config;
                    },
                    response: function (response) {
                        if (response.hasOwnProperty('data')) {
                            if(response.data.c=="u"){
                                var userInfo = JSON.parse(localStorage.getItem('userInfo'));
                                if(userInfo){
                                    localStorage.setItem("orginUser",userInfo.userName);
                                    localStorage.setItem("orginUrl",window.location.href);
                                }

                                window.location = '#/login';
                            }else{
                                return {code: response.status, data: response.data};
                            }

                        } else {
                            return response;
                        }
                    }
                }
            }]);

        }]).value('api', "http://192.168.2.207:8080/mam_cp")

    // .value('api', "http://192.168.2.195:8080/mam_cp")

/* }]).value('api', "http://192.168.2.161:8081/AGX_MAM_CP_API_V1")*/
